package com.inspinia.base.api.controller;

import com.inspinia.base.api.model.InterfaceUserExternal;
import com.inspinia.base.api.service.InterfaceUserExternalService;
import com.inspinia.base.common.model.AjaxResult;
import com.inspinia.base.pagination.Pager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
* 的Controller
* Created by pujw on 2017-6-30.
*/
@Controller
@RequestMapping("interfaceUserExternal")
public class InterfaceUserExternalController {

    @Resource
    private InterfaceUserExternalService interfaceUserExternalService;

    /**
    * 跳转分页页面
    *
    * @return
    */
    @RequestMapping(value = "paging", method = RequestMethod.GET)
    public String toPaginationPage() {
        return "interfaceUserExternal/interfaceUserExternal-pagination";
    }

    /**
    * 分页查询
    *
    * @return
    */
    @ResponseBody
    @RequestMapping(value = "paging", method = RequestMethod.POST)
    public AjaxResult paging(InterfaceUserExternal interfaceUserExternal) {
        Pager<InterfaceUserExternal> page = interfaceUserExternalService.findPage(interfaceUserExternal);
        return new AjaxResult(true, page);
    }

    /**
    * 创建
    *
    * @param interfaceUserExternal
    * @return
    */
    @ResponseBody
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public AjaxResult create(@Valid InterfaceUserExternal interfaceUserExternal) {
        interfaceUserExternalService.insert(interfaceUserExternal);
        return new AjaxResult(true);
    }

    /**
    * 根据id获取
    *
    * @param id id
    * @return
    */
    @ResponseBody
    @RequestMapping(value = "get", method = RequestMethod.GET)
    public AjaxResult get(long id) {
        InterfaceUserExternal bean = interfaceUserExternalService.get(id);
        return new AjaxResult(true, bean);
    }

    /**
    * 编辑
    *
    * @param interfaceUserExternal
    * @return
    */
    @ResponseBody
    @RequestMapping(value = "goEdit", method = RequestMethod.POST)
        public AjaxResult edit(@Valid InterfaceUserExternal interfaceUserExternal) {
        interfaceUserExternalService.update(interfaceUserExternal);
        return new AjaxResult(true);
    }

    /**
    * 删除
    *
    * @param id
    * @return
    */
    @ResponseBody
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public AjaxResult delete(Long id) {
        int deleted = interfaceUserExternalService.delete(id);
        if( deleted > 0 ){
        	return new AjaxResult(true);
        }
        return new AjaxResult(false,"删除失败，请重试！");
    }
    
    /**
    * 批量删除
    *
    * @param ids
    * @return
    */
    @ResponseBody
    @RequestMapping(value = "batchDelete", method = RequestMethod.POST)
    public AjaxResult delete(@RequestParam(name = "ids[]", required = false, defaultValue = "") Long[] ids) {
        int deleted =  interfaceUserExternalService.batchDelete(ids);
        if( deleted > 0 && ids.length == deleted ){
        	return new AjaxResult(true);
        }
        return new AjaxResult(false,"有"+(ids.length - deleted)+"个资源删除失败，请重试！");
    }


}